package ${t.packagePath}.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import cn.hutool.core.bean.BeanUtil;
import java.util.stream.Collectors;
import ${t.packagePath}.utils.AjaxJson;
import ${t.packagePath}.config.StpSpUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.ApiOperation;
import ${t.packagePath}.service.${t.userLoginTableName}Service;
import ${t.packagePath}.service.${t.userTableName}Service;
import ${t.packagePath}.model.${t.userLoginTableName};
import ${t.packagePath}.model.${t.userTableName};
<#if cfg.saTokenAuthWay == 1 >import cn.dev33.satoken.stp.StpUtil;
<#else>import cn.dev33.satoken.annotation.SaCheckPermission;
</#if>


/**
 * Controller: ${t.tableName} -- ${t.tableComment}
 * @author ${cfg.author} 
 */
@RestController
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class ${t.userLoginTableName}Controller {

	${t.userLoginTableName}Service ${t.userLoginTableName?uncap_first}Service;
	${t.userTableName}Service ${t.userTableName?uncap_first}Service;
	BeanSearcher searcher;

	@ApiOperation("删")
	@${cfg.apiMappingWayString}("/SpAdminLogin/delete")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.userLoginTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson delete(${t.primaryKey.fieldType} id){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.userLoginTableName}.PERMISSION_CODE);${cfg.line}</#if><#rt>

		boolean b = ${t.userLoginTableName?uncap_first}Service.removeById(id);
		return AjaxJson.getByBoolean(b);
	}
	
	@ApiOperation("删-批量")
	@${cfg.apiMappingWayString}("/SpAdminLogin/deleteByIds")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.userLoginTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson deleteByIds(HttpServletRequest request){
<#if cfg.saTokenAuthWay == 1 >        StpUtil.checkPermission(${t.userLoginTableName}.PERMISSION_CODE);${cfg.line}</#if><#rt>
<#if t.hasFo("logic-delete")>
		int line = ${t.varName}Service.updateColumnByIds(${t.userLoginTableName}.TABLE_NAME, "${t.getDbColumnByFoType('logic-delete').columnName}", ${t.getDbColumnByFoType('logic-delete').tx.no}, ids);
<#else>
		Map<String, Object> flat = MapUtils.flat(request.getParameterMap());
        Object object = flat.get("ids");
        if (object == null) {
            return AjaxJson.getSuccess();
        }
        List<Long> ids = Arrays.stream(object.toString().split(",")).map(Long::parseLong).collect(Collectors.toList());
		boolean b = ${t.userLoginTableName?uncap_first}Service.removeByIds(ids);
</#if>
		return AjaxJson.getByBoolean(b);
	}


	@ApiOperation("查")
	@${cfg.apiMappingWayString}("/SpAdminLogin/getById")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.userLoginTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
		public AjaxJson getById(${t.primaryKey.fieldType} id){
		${t.userLoginTableName} ${t.userLoginTableName?uncap_first} = ${t.userLoginTableName?uncap_first}Service.getById(id);
		return AjaxJson.getSuccessData(${t.userLoginTableName?uncap_first});
	}



	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	@${cfg.apiMappingWayString}("/SpAdminLogin/getList")
<#if cfg.saTokenAuthWay == 2 >    @SaCheckPermission(type= Stp${t.userTableName}Util.TYPE,value=${t.userLoginTableName}.PERMISSION_CODE)${cfg.line}</#if><#rt>
	public AjaxJson getList(HttpServletRequest request) {
		SearchResult<${t.userLoginTableName}> search = searcher.search(${t.userLoginTableName}.class, MapUtils.flat(request.getParameterMap()));
		//补全用户信息
		List<Map<String, Object>> collect = search.getDataList()
				.stream()
				.map(BeanUtil::beanToMap)
				.collect(Collectors.toList());
		for (Map<String, Object> map : collect) {
			SpUser user = ${t.userTableName?uncap_first}Service.getById(map.get("accId").toString());
			map.put("avatar",user.getAvatar());
			map.put("name",user.getName());
		}
    	return AjaxJson.getPageData(search.getTotalCount().longValue(), search.getDataList());
	}
}
